<?php
namespace App\Services\Alipay;

use Illuminate\Support\ServiceProvider;
use Illuminate\Foundation\Application as LaravelApplication;
use Laravel\Lumen\Application as LumenApplication;

class AlipayAppServiceProvider extends ServiceProvider
{

    protected $defer = true;

    /**
     * Register the service provider.
     *
     * @return void
     */
    public function register()
    {
        
        $this->app->bind('alipay.app', function ($app)
        {
            require 'libs/AopSdk.php';

            $aop = new \AopClient;
            $aop->gatewayUrl = "https://openapi.alipay.com/gateway.do";
            $aop->appId = $app->config->get('alipay-app.appid');
            $aop->rsaPrivateKey = $app->config->get('alipay-app.app_private_key');
            $aop->format = "json";
            $aop->charset = "UTF-8";
            $aop->signType = "RSA2";
            $aop->alipayrsaPublicKey = $app->config->get('alipay-app.alipay_rsa_public_key');
            //实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称：alipay.trade.app.pay
            $request = new \AlipayTradeAppPayRequest();
            //SDK已经封装掉了公共参数，这里只需要传入业务参数
            $request->setNotifyUrl( $app->config->get('alipay-app.notify_url') );

            
            $alipayAppPay = new AlipayAppPay($aop, $request);
            $alipayAppPay->rsaPublicKey = $app->config->get('alipay-app.app_public_key');
            return $alipayAppPay;
        });

        $this->app->bind('alipay.refund', function ($app)
        {
            require 'libs/AopSdk.php';
            require 'libs/pagepay/buildermodel/AlipayTradeRefundContentBuilder.php';
            $aop = new \AopClient;

            $aop->gatewayUrl = "https://openapi.alipay.com/gateway.do";
            $aop->appId = $app->config->get('alipay-app.appid');
            $aop->method="alipay.trade.refund";
            $aop->apiVersion = '1.0';
            $aop->format = "json";
            $aop->charset = "UTF-8";
            $aop->signType = "RSA2";
            $aop->timestamp=date("Y-m-d H:i:s",time());
            $aop->appId = $app->config->get('alipay-app.appid');
            $aop->rsaPrivateKey = $app->config->get('alipay-app.app_private_key');
            $aop->alipayrsaPublicKey = $app->config->get('alipay-app.alipay_rsa_public_key');
            return $aop;
            
        });


    }

    /**
     * Get the services provided by the provider.
     *
     * @return array
     */
    public function provides()
    {
        return [
            'alipay.app',
            'alipay.refund',
        ];
    }
}
